﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;

namespace CTS.Com.Domain.Validate.Model
{
    [Serializable]
    [XmlRoot(ElementName = "Rules")]
    public class Rules : IList<Rule>
    {
        public IList<Rule> ListRules = new List<Rule>();

        /// <summary>
        /// Gets or sets the element at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index of the element to get or set.</param>
        /// <returns>The element at the specified index.</returns>
        public Rule this[int index]
        {
            get { return ListRules[index]; }
            set { ListRules[index] = value; }
        }

        /// <summary>
        /// Gets or sets the element at the specified name
        /// </summary>
        /// <param name="name">The name of the element to get or set.</param>
        /// <returns>The element at the specified name.</returns>
        public Rule this[string name]
        {
            get { return (from tbl in ListRules where tbl.Name == name select tbl).SingleOrDefault(); }
        }

        /// <summary>
        /// Gets the number of elements contained in the System.Collections.Generic.ICollection<T>.
        /// </summary>
        public int Count { get { return ListRules.Count; } }

        /// <summary>
        /// Gets a value indicating whether the System.Collections.Generic.ICollection<T> is read-only.
        /// </summary>
        public bool IsReadOnly { get { return ListRules.IsReadOnly; } }

        /// <summary>
        /// Determines the elements of the System.Collections.Generic.IList<T> is empty
        /// </summary>
        public bool IsEmpty { get { return (ListRules == null || ListRules.Count <= 0); } }

        /// <summary>
        /// Determines the index of a specific item in the System.Collections.Generic.IList<T>.
        /// </summary>
        /// <param name="item">The object to locate in the System.Collections.Generic.IList<T>.</param>
        /// <returns>The index of item if found in the list; otherwise, -1.</returns>
        public int IndexOf(Rule item)
        {
            return ListRules.IndexOf(item);
        }

        /// <summary>
        /// Inserts an item to the System.Collections.Generic.IList<T> at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which item should be inserted.</param>
        /// <param name="item">The object to insert into the System.Collections.Generic.IList<T>.</param>
        public void Insert(int index, Rule item)
        {
            ListRules.Insert(index, item);
        }

        /// <summary>
        /// Removes the System.Collections.Generic.IList<T> item at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index of the item to remove.</param>
        public void RemoveAt(int index)
        {
            ListRules.RemoveAt(index);
        }

        /// <summary>
        /// Adds an item to the System.Collections.Generic.ICollection<T>.
        /// </summary>
        /// <param name="item">The object to add to the System.Collections.Generic.ICollection<T>.</param>
        public void Add(Rule item)
        {
            ListRules.Add(item);
        }

        /// <summary>
        /// Removes all items from the System.Collections.Generic.ICollection<T>.
        /// </summary>
        public void Clear()
        {
            ListRules.Clear();
        }

        /// <summary>
        /// Determines whether the System.Collections.Generic.ICollection<T> contains a specific value.
        /// </summary>
        /// <param name="item">The object to locate in the System.Collections.Generic.ICollection<T>.</param>
        /// <returns>true if item is found in the System.Collections.Generic.ICollection<T>; otherwise, false.</returns>
        public bool Contains(Rule item)
        {
            return ListRules.Contains(item);
        }

        /// <summary>
        /// Copies the elements of the System.Collections.Generic.ICollection<T> to an System.Array, starting at a particular System.Array index.
        /// </summary>
        /// <param name="array">The one-dimensional System.Array that is the destination of the elements copied from System.Collections.Generic.ICollection<T>. The System.Array must have zero-based indexing.</param>
        /// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
        public void CopyTo(Rule[] array, int arrayIndex)
        {
            ListRules.CopyTo(array, arrayIndex);
        }

        /// <summary>
        /// Removes the first occurrence of a specific object from the System.Collections.Generic.ICollection<T>.
        /// </summary>
        /// <param name="item">The object to remove from the System.Collections.Generic.ICollection<T>.</param>
        /// <returns>true if item was successfully removed from the System.Collections.Generic.ICollection<T>; otherwise, false. This method also returns false if item is not found in the original System.Collections.Generic.ICollection<T>.</returns>
        public bool Remove(Rule item)
        {
            return ListRules.Remove(item);
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>A System.Collections.Generic.IEnumerator<T> that can be used to iterate through the collection.</returns>
        public IEnumerator<Rule> GetEnumerator()
        {
            return ListRules.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>A System.Collections.Generic.IEnumerator<T> that can be used to iterate through the collection.</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ListRules.GetEnumerator();
        }
    }
}
